"""
一个从建立Graph到插入Fuseki的完整过程的测试
@Time: 2020/10/24 21:26
@Author: yb
@File: complete_test.py
"""

import unittest
from rdflib import Graph, Literal
from rdflib.namespace import XSD

from src.spiders.common.jena_fuseki import FusekiUpdate
from src.spiders.common.rdf_prefix import RdfPrefix as rp
from src.spiders.common.propertyIRI import ObjectPropertyIRI as op
from src.spiders.common.propertyIRI import DataPropertyIPI as dp
from src.spiders.common.utils import RdfUtils


class CompleteTest(unittest.TestCase):
    """完整测试的类"""

    def setUp(self) -> None:
        self.fuseki = FusekiUpdate('http://localhost:7999', 'test_db')
        self.g = Graph()


    def test_complete(self):
        """测试整个过程"""
        RdfUtils.bind_prefixes_to_graph(self.g, [rp.BrandProject, rp.Firm])  # 绑定前缀
        # 假设获取的数据为rev_data
        rev_data = {
            'band_project': '腾讯',
            '所属企业': '深圳市腾讯计算机系统有限公司',
            '成立日期': '1998-11-11',
            '英文名称': 'QQ'
        }
        # 将rev_data转化成RDF三元组并加入graph中
        brand_project_instance = rp.BrandProject.value[rev_data['band_project']]
        self.g.add(
            (brand_project_instance, op.subordinateTo.value, rp.Firm.value[rev_data['所属企业']])
        )
        self.g.add(
            (brand_project_instance, dp.createTime.value, Literal(rev_data['成立日期'], datatype=XSD.date))
        )
        self.g.add(
            (brand_project_instance, dp.enName.value, Literal(rev_data['英文名称'], datatype=XSD.string))
        )

        # 将graph插入
        self.fuseki.insert_graph(self.g)


if __name__ == '__main__':
    unittest.main()
